package middleware

import (
	"github.com/gin-gonic/gin"
)

func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		origin := c.Request.Header.Get("origin")
		//fmt.Printf("============header-origin: %s\n", origin)
		c.Header("access-control-allow-credentials", "true")
		c.Header("access-control-allow-headers", "X-Requested-With,Content-Type,auth-token,X-Log-Id,sw8,token,Content-Length,Accept-Encoding,authorization, Cache-Control")
		c.Header("access-control-allow-methods", "GET,POST,OPTIONS,PUT,HEAD")
		c.Header("access-control-max-age", "600")
		c.Header("Access-Control-Allow-Origin", origin)

		if c.Request.Method == "OPTIONS" {
			//stark.Logger.Infof(c, "option-request...")
			c.Abort()
			// c.AbortWithStatus(204)
			c.JSON(200, "ok!")
			return
		}

		c.Next()
	}
}
